package com.example.leave;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;

public class StudentLeave {
    private String studentId;
    private JLabel statusLabel; // 状态标签
    private List<LeaveRequest> leaveRequests = new ArrayList<>(); // 保存请假记录

    private static Counselor counselor; // 添加Counselor对象

    public StudentLeave(String studentId, Counselor counselor) {
        this.studentId = studentId;
        this.counselor = counselor; // 初始化Counselor对象
        createAndShowGUI();
    }

    private void createAndShowGUI() {
        // 创建主窗口
        JFrame frame = new JFrame("请假页面");

        // 创建面板
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        // 创建查询面板
        JPanel queryPanel = new JPanel();
        queryPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        JLabel idLabel = new JLabel("学号：");
        JTextField idText = new JTextField(10);
        idText.setText(studentId);

        // 日期选择器
        JLabel startDateLabel = new JLabel("开始时间：");
        JSpinner startSpinner = new JSpinner(new SpinnerDateModel());
        JSpinner.DateEditor startEditor = new JSpinner.DateEditor(startSpinner, "yyyy.MM.dd HH:mm");
        startSpinner.setEditor(startEditor);

        JLabel endDateLabel = new JLabel("结束时间：");
        JSpinner endSpinner = new JSpinner(new SpinnerDateModel());
        JSpinner.DateEditor endEditor = new JSpinner.DateEditor(endSpinner, "yyyy.MM.dd HH:mm");
        endSpinner.setEditor(endEditor);

        // 请假原因文本框
        JLabel reasonLabel = new JLabel("请假原因：");
        JTextArea reasonTextArea = new JTextArea(40, 50);
        reasonTextArea.setLineWrap(true);
        reasonTextArea.setWrapStyleWord(true);

        // 状态标签
        statusLabel = new JLabel("状态：未申请");

        // 查看和修改按钮
        JPanel actionPanel = new JPanel();
        actionPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); // 居中对齐
        JButton leaveButton = new JButton("申请");
        JButton editButton = new JButton("修改");
        JButton cancelButton = new JButton("取消");

        leaveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            	 // 获取输入数据
                String startTime = new SimpleDateFormat("yyyy.MM.dd HH:mm").format(startSpinner.getValue());
                String endTime = new SimpleDateFormat("yyyy.MM.dd HH:mm").format(endSpinner.getValue());
                String reason = reasonTextArea.getText();
                
                // 更新状态
                statusLabel.setText("状态：已申请");
//                
//                // 将数据传递给Counselor
//                counselor.addLeaveRequest(studentId, startTime, endTime, reason, "已申请");
                LeaveRequest leaveRequest = new LeaveRequest(studentId, startTime, endTime, reason,"已申请");
                leaveRequests.add(leaveRequest);

                if (counselor != null) {
                    counselor.addLeaveRequest(studentId, startTime, endTime, reason, "已申请");
                }
            }
        });
        // 创建取消按钮并添加事件监听器
//        JButton cancelButton = new JButton("取消");
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            	reasonTextArea.setText("");
            	frame.dispose();
            }
        });
        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new LeaveRequestPage(leaveRequests); // 打开新的页面并传递数据
            }
        });


        // 添加组件到面板
        queryPanel.add(idLabel);
        queryPanel.add(idText);
        queryPanel.add(startDateLabel);
        queryPanel.add(startSpinner);
        queryPanel.add(endDateLabel);
        queryPanel.add(endSpinner);
        queryPanel.add(reasonLabel);
        queryPanel.add(new JScrollPane(reasonTextArea)); // 添加滚动条

        // 添加按钮到动作面板
        actionPanel.add(leaveButton);
        actionPanel.add(editButton);
        actionPanel.add(cancelButton);
        actionPanel.add(statusLabel); // 添加状态标签

        // 添加组件到主面板
        panel.add(queryPanel);
        panel.add(actionPanel);

        // 设置窗口属性
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(550, 300);
        frame.add(panel);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
//    	Counselor counselor = new Counselor(studentId,counselor);
        SwingUtilities.invokeLater(() -> new StudentLeave("12345", counselor));
    }
}
